﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using XiAnAirport.DAL;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class FaultRepairBUS
    {
        public ResponseMessage AddFaultRepairType(FaultRepairType faultRepairType)
        {
            FaultRepairDAL da = new FaultRepairDAL();
            ResponseMessage message = new ResponseMessage();

            if (faultRepairType.faultType.Contains(','))
            {
                message.errcode = 1;
                message.errmsg = "故障类别名称不能包含英文逗号";
            }
            else
            {
                string result = da.AddFaultRepairType(faultRepairType);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }

            return message;
        }

        public ResponseMessage EditFaultRepairType(FaultRepairType faultRepairType)
        {
            FaultRepairDAL da = new FaultRepairDAL();
            ResponseMessage message = new ResponseMessage();
            if (faultRepairType.faultType.Contains(','))
            {
                message.errcode = 1;
                message.errmsg = "故障类别名称不能包含英文逗号";
            }
            else
            {
                string result = da.EditFaultRepairType(faultRepairType);

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }

            return message;
        }

        public ResponseMessage DelFaultRepairType(FaultRepairType faultRepairType)
        {
            FaultRepairDAL da = new FaultRepairDAL();
            ResponseMessage message = new ResponseMessage();
            string result = da.DelFaultRepairType(faultRepairType);
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage LoadFaultRepairType(FaultRepairType faultRepairType)
        {
            FaultRepairDAL da = new FaultRepairDAL();
            ResponseMessage message = new ResponseMessage();
            FaultRepairType content = da.LoadFaultRepairType(faultRepairType.id);
            if (content != null)
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "未查询到相关信息";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryFaultRepairType(FaultRepairType faultRepairType)
        {
            ResponseMessage message = new ResponseMessage();

            FaultRepairDAL da = new FaultRepairDAL();
            List<FaultRepairType> content = da.QueryFaultRepairType(faultRepairType);
            message.data = content;
            message.total = da.QueryFaultRepairTypeCount(faultRepairType);
            return message;
        }

        public ResponseMessage CleanUpRepairTyper()
        {
            ResponseMessage message = new ResponseMessage();
            FaultRepairDAL da = new FaultRepairDAL();
            string result = string.Empty;
            try
            {
                result = da.CleanUpFaultRepairType();

                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage BatchDelFaultRepairType(string ids)
        {
            FaultRepairDAL da = new FaultRepairDAL();
            ResponseMessage message = new ResponseMessage();
            string result = da.BatchDelFaultRepairType(ids);
            if (result.Contains("success"))
            {
                message.errcode = 0;
                message.errmsg = "";
                message.data = null;
                message.total = 1;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = result;
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage AddDeviceWorkOrder(string LoginName, string Reporttime, string opcNO, string Fault_Type, string Fault_Cause, string IsTiring)
        {
            ResponseMessage message = new ResponseMessage();
            RepairWorkOrderDAL da = new RepairWorkOrderDAL();
            FaultRepairDAL fda = new FaultRepairDAL();
            RepairWorkOrderInfo workorderinfo = new RepairWorkOrderInfo();
            ElevatorAreaDAL eda = new ElevatorAreaDAL();
            int orderid = 0;
            try
            {
                Elevator elevator = eda.QueryElevatorByOpc(opcNO);
                if (elevator == null)
                {
                    message.errcode = 1;
                    message.errmsg = "未查询到电梯信息";
                    message.data = "";
                    message.total = 1;
                    return message;
                }
                workorderinfo.elevatorId = elevator.id;
                workorderinfo.creator = LoginName;
                workorderinfo.reportPerson = LoginName;
                workorderinfo.faultType = Fault_Type;
                workorderinfo.faultCause = Fault_Cause;
                workorderinfo.reportTime = Reporttime;
                workorderinfo.isTiring = int.Parse(IsTiring);
                string result = da.AddDeviceFalutWorkOrder(workorderinfo);
                if (result.Contains("success"))
                {
                    message.errcode = 0;
                    message.errmsg = "";
                    message.data = null;
                    message.total = 1;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = result;
                    message.data = null;
                    message.total = 0;
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = e.ToString();
                message.data = "";
                message.total = 0;
            }
            return message;
        }

        public ResponseMessage QueryAllFaultType()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                FaultRepairDAL da = new FaultRepairDAL();
                List<FaultRepairType> content = da.QueryAllFaultType();
                message.data = content;
            }
            catch
            {
                message.errcode = 1;
            }
            return message;


        }

        public ResponseMessage QueryAllFaultReason()
        {
            ResponseMessage message = new ResponseMessage();

            FaultRepairDAL da = new FaultRepairDAL();
            List<FaultRepairType> content = da.QueryAllFaultType();
            foreach (var item in content)
            {
                item.child = da.QueryFaultReasonByType(item.faultType);
            }
            message.data = content;
            return message;
        }
    }
}
